package com.booktrade.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.booktrade.entity.Orders;
import com.booktrade.entity.Reviews;
import com.booktrade.entity.Users;
import com.booktrade.result.Result;
import com.booktrade.service.IOrdersService;
import com.booktrade.service.IReviewsService;
import com.booktrade.service.IUsersService;
import com.booktrade.util.BaseContext;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 评价表，存储用户对交易的评价信息 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/reviews")
@RequiredArgsConstructor
public class ReviewsController {
    private final IReviewsService reviewsService;

    /**
     * 、评价功能：
     * 1)	用户对交易进行综合评价，评价分为”好评”、“中评”、“差评”三类。
     * 评价转化为积分，具体为：“好评”加一分，“中评”零分，“差评”扣一分。累计积分作为卖家信用分显示在卖家信息中。
     */
    @PostMapping("/add")
    @Operation(summary = "添加评价")
    public Result addReviews(@RequestBody Reviews reviews) {
        Integer reviewerId = BaseContext.getCurrentId();
       reviewsService.addReviews(reviews, reviewerId);
        return Result.success();
    }

    @GetMapping("/list")
    @Operation(summary = "获取评价列表")
    public Result<List<Reviews>> list() {
        Integer reviewerId = BaseContext.getCurrentId();
        List<Reviews> reviewsList = reviewsService.list(new LambdaQueryWrapper<Reviews>().eq(Reviews::getReviewerId, reviewerId));
        return Result.success(reviewsList);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取评价详情")
    public Result<Reviews> getById(@PathVariable("id") Integer id) {
        Reviews reviews = reviewsService.getOne(new LambdaQueryWrapper<Reviews>().eq(Reviews::getOrderId, id));
        return Result.success(reviews);
    }

}
